package cat.service.impl;

import cat.entity.Article;
import cat.entity.ArticleTagRef;
import cat.entity.Tag;
import cat.mapper.ArticleTagRefMapper;
import cat.mapper.TagMapper;
import cat.service.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Resource
    TagMapper tagMapper;

    @Resource
    ArticleTagRefMapper articleTagRefMapper;

    public List<Tag> listTag() {
        return tagMapper.listTag();
    }

    public List<Tag> selectAllTag() {

        List<Tag> tagList = tagMapper.selectAllTag();
        for (Tag tag : tagList) {
            List<Article> articleList = articleTagRefMapper.listArticleByTagId(tag.getTagId());
            tag.setArticleCount(articleList.size());
        }
        return tagList;
    }

    public void deleteTagById(Integer TagId) {
        tagMapper.deleteTagById(TagId);
    }

    public void addTag(Tag tag) {
        tagMapper.addTag(tag);
    }

    public List<Tag> selectTagById(Integer tagId) {
        return tagMapper.selectTagById(tagId);
    }

    public void updateTag(Tag tag) {
        tagMapper.updateTag(tag);
    }
}
